package com.cy.pj.common.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class SysTimeAspect {
    @Pointcut("bean(sysUserServiceImpl)")
    public void doTime(){}
    @Before("doTime()")
    public void doBefore(JoinPoint joinPoint){
        System.out.println("time doBefore()");
    }
    @After("doTime()")
    public void doAfter(){
        System.out.println("time doAfter()");
    }
    /**核心业务正常结束时执行* 说明：假如有after，先执行after,再执行returning 也和版本有关
     spring4.0以及以前是 after > returning/throwing 5.28以后是 returning/throwing > after
     */
    @AfterReturning("doTime()")
    public void doAfterReturning(){
        System.out.println("time doAfterReturning");
    }
    /**核心业务出现异常时执行说明：假如有after，先执行after,再执行Throwing*/
    @AfterThrowing("doTime()")
    public void doAfterThrowing(){
        System.out.println("time doAfterThrowing");
    }
    @Around("doTime()")
    public Object doAround(ProceedingJoinPoint jp) throws Throwable{
        System.out.println("doAround.before");
        Object obj;
        try{
            //假如有before通知先执行before,没有则目标方法
            obj=jp.proceed();
            System.out.println("doAround.after");
        }catch(Throwable e){
            System.out.println(e.getMessage());
            throw e;
        }
        return obj;
    }
}